﻿using Unity.Entities;
using Unity.Rendering;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Burst;

[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
[UpdateInGroup(typeof(PostBakingSystemGroup))]
partial struct AnimationDataHolderBakingSystem : ISystem
{

    public void OnUpdate(ref SystemState state)
    {
        AnimationDatalListSO animationDataListSO = null;
        foreach (RefRO<AnimationDataHolderObjectData> animationDataHolderObjectData in SystemAPI.Query<RefRO<AnimationDataHolderObjectData>>())
        {
            animationDataListSO = animationDataHolderObjectData.ValueRO.animationDataListSO.Value;
        }

        //对词典数据申请空间
        Dictionary<AnimationDataSO.AnimationType, int[]> blobAssetDataDictionary = new Dictionary<AnimationDataSO.AnimationType, int[]>();
        foreach (AnimationDataSO.AnimationType animationType in System.Enum.GetValues(typeof(AnimationDataSO.AnimationType)))
        {
            AnimationDataSO animationDataSO = animationDataListSO.GetAnimationDataSO(animationType);
            blobAssetDataDictionary[animationType] = new int[animationDataSO.meshArray.Length];

            //UnityEngine.Debug.Log("Animation.........................");
        }

        //将Mesh id信息 与对应的动画ID绑定起来
        foreach ((RefRO<AnimationDataHolderSubEntity> animationDataHolderSubEntity, RefRO<MaterialMeshInfo> materialMeshInfo)
        in SystemAPI.Query<RefRO<AnimationDataHolderSubEntity>, RefRO<MaterialMeshInfo>>())
        {
            blobAssetDataDictionary[animationDataHolderSubEntity.ValueRO.animationType]
                [animationDataHolderSubEntity.ValueRO.meshIndex] = materialMeshInfo.ValueRO.Mesh;

            //UnityEngine.Debug.Log(animationDataHolderSubEntity.ValueRO.animationType + "::"
            //    + animationDataHolderSubEntity.ValueRO.meshIndex + " = " +
            //    materialMeshInfo.ValueRO.Mesh);
        }

        foreach (RefRW<AnimationDataHolder> animationDataHolder in SystemAPI.Query<RefRW<AnimationDataHolder>>())
        {
            //构建Blob 数组
            BlobBuilder blobBuilder = new BlobBuilder(Allocator.Temp);
            ref BlobArray<AnimationData> animationDataBlobArray = ref blobBuilder.ConstructRoot<BlobArray<AnimationData>>();//忘记使用 ref 关键字可能导致使用的是数据的副本


            BlobBuilderArray<AnimationData> animationdAtaBlobArray
                = blobBuilder.Allocate<AnimationData>(ref animationDataBlobArray, System.Enum.GetValues(typeof(AnimationDataSO.AnimationType)).Length);

            int index = 0;
            //Blob学习
            foreach (AnimationDataSO.AnimationType animationType in System.Enum.GetValues(typeof(AnimationDataSO.AnimationType)))
            {
                AnimationDataSO animationDataSO = animationDataListSO.GetAnimationDataSO(animationType);

                BlobBuilderArray<int> blobBuilderArray =
                  blobBuilder.Allocate<int>(ref animationdAtaBlobArray[index].intMeshIdBlobArray, animationDataSO.meshArray.Length);

                animationdAtaBlobArray[index].frameTimerMax = animationDataSO.frameTimerMax;
                animationdAtaBlobArray[index].frameMax = animationDataSO.meshArray.Length;

                for (int i = 0; i < animationDataSO.meshArray.Length; i++)
                {
                    blobBuilderArray[i] = blobAssetDataDictionary[animationType][i];
                }
                index++;
            }

            animationDataHolder.ValueRW.animationDataBlobArrayBlobAssetReference =
            blobBuilder.CreateBlobAssetReference<BlobArray<AnimationData>>(Allocator.Persistent);

            //释放blobBuilder构建器(忘记了会导致内存泄漏)
            blobBuilder.Dispose();
        }
    }


    public void OnDestroy(ref SystemState state)
    {
        foreach (RefRW<AnimationDataHolder> animationDataHolder in SystemAPI.Query<RefRW<AnimationDataHolder>>())
        {
            animationDataHolder.ValueRW.animationDataBlobArrayBlobAssetReference.Dispose();
        }
    }
}